<!--
 * @Description: 资讯分类页面
 * @Author: Ronda
 * @Date: 2021-03-30 11:10:36
 * @LastEditors: Ronda
 * @LastEditTime: 2021-04-07 21:28:46
-->
<template>
  <div>
    <!-- 模态框 -->
    <el-dialog title="新增咨询分类" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="栏目名称" label-width="120px" required>
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="栏目描述" label-width="120px">
          <el-input v-model="form.description" autocomplete="off" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="序号" label-width="120px">
          <el-select v-model="form.no" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="父栏目" label-width="120px">
          <el-input v-model="form.parentId" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="toAdd">确 定</el-button>
      </div>
    </el-dialog>

    <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
    <!-- {{categoryData}} -->
    <el-table :data="categoryData" style="width: 100%">
      <el-table-column prop="name" label="名称">
      </el-table-column>
      <el-table-column prop="description" label="描述">
      </el-table-column>
      <el-table-column type="index" prop="no" label="序号">
      </el-table-column>
      <el-table-column prop="parentId" label="父栏目">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="medium" type="primary" icon="el-icon-edit" circle @click="handleEdit(scope.$index, scope.row)"></el-button>
          <el-button size="medium" type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.$index, scope.row)"></el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
  import { mapState, mapActions } from 'vuex';
  export default {
    data () {
      return {
        dialogFormVisible: false,
        form: {
          name: '',
          description: '',
          no: '',
          parentId: ''
        },

      };
    },
    computed: {
      ...mapState('category', ['categoryData'])
    },
    methods: {
      ...mapActions('category', ['findAllCategory', 'upadteOrSave', 'deleteById']),


      toAdd () {
        if (this.form.name == '' || this.form.name == null) this.$message.error('栏目名称不能为空！');
        else {
          this.upadteOrSave(this.form);
          this.$message.success('新增成功！');
          this.findAllCategory();
          this.dialogFormVisible = false;
        }
      },

      /**
       * @description:编辑按钮
       * @param {*} row:当前的栏目对象
       * @return {*}
       */
      handleEdit (index, row) {
        this.form = row;
        this.dialogFormVisible = true
        console.log(index, row);
      },

      /**
       * @description:  通过id删除
       * @param {*} index
       * @param {*} row
       * @return {*}
       */
      handleDelete (index, row) {
        console.log(index, row);
        this.deleteById(row);
        this.$message.success('删除成功！');
        this.findAllCategory();

      }
    },
    created () {
      this.findAllCategory();
    },
    mounted () { },
  };
</script>
<style scoped>
</style>